package Internet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    private Socket  socket=null;
    public TcpEchoClient(String serverIp,int serverPort) throws IOException {
        socket=new Socket(serverIp,serverPort);
    }
    public void start() throws IOException {
            System.out.println("请输入请求");
            Scanner scanner=new Scanner(System.in);
            try(InputStream inputStream=socket.getInputStream();
                OutputStream outputStream=socket.getOutputStream())
                {
                    while(true){
                        Scanner scanner1=new Scanner(inputStream);
                        PrintWriter printWriter=new PrintWriter(outputStream);
                        String resquest=scanner.next();
                        //1.发送请求
                        printWriter.println(resquest);
                        //printWriter.write(resquest);
                        printWriter.flush();
                        //2.接收响应
                        String response=scanner1.next();
                        //3.输出结果
                        System.out.println(response);
                    }
                //包装一下，为了操作方便

            }



    }
//public void start() throws IOException {
//    System.out.println("请输入请求");
//    Scanner scanner=new Scanner(System.in);
//
//        while(true){
//            InputStream inputStream=socket.getInputStream();
//                OutputStream outputStream=socket.getOutputStream();
//            {
//            Scanner scanner1=new Scanner(inputStream);
//            PrintWriter printWriter=new PrintWriter(outputStream);
//            String resquest=scanner.next();
//            //1.发送请求
//            printWriter.println(resquest);
//            //printWriter.write(resquest);
//            printWriter.flush();
//            //2.接收响应
//            String response=scanner1.next();
//            //3.输出结果
//            System.out.println(response);
//        }
//        //包装一下，为了操作方便
//
//    }



//}
    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient=new TcpEchoClient("127.0.0.1",9090);
        tcpEchoClient.start();

    }
}
